package bom.service.impl.organization;


import bom.configuration.constant.SystemStaticConst;
import bom.dao.organization.OrgGroupDao;
import bom.pojo.organization.OrgGroup;
import bom.service.i.organization.OrgGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Fancyears·Maylos·Malvis
 * @Description:
 * @Date: Created in  2018/11/27 15:24
 * @Modified By:
 */
@Service
public class OrgGroupServiceImpl implements OrgGroupService {
    @Autowired(required = false)
    private OrgGroupDao orgGroupDao;

    public String getMaxOrgGroup(String parentNode) {
        return orgGroupDao.getMaxOrgGroup(parentNode);
    }

    public OrgGroup findByNode(String node) {
        return orgGroupDao.findByNode(node);
    }

    public Integer findOrgGroupUserCount(Long id) {
        return orgGroupDao.queryOrgGroupUserCount(id);
    }

    @Override
    public OrgGroup get(OrgGroup update) {
        return null;
    }

    @Override
    public boolean update(OrgGroup update) {
        return false;
    }

    @Override
    public Map<String, Object> updateOrg(OrgGroup entity) {
        Map<String, Object> result = new HashMap<String, Object>();
        OrgGroup update = new OrgGroup();
        update.setGroupId(entity.getGroupId());
        update = get(update);
        update.setName(entity.getName());
        update.setGroupCode(entity.getGroupCode());
        update.setNum(entity.getNum());
        boolean success = update(update);
        if (success) {
            result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
            result.put(SystemStaticConst.MSG, "修改数据成功！");
            result.put("entity", entity);
        } else {
            result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
            result.put(SystemStaticConst.MSG, "修改数据失败！");
        }
        return result;
    }

    @Override
    public Map<String, Object> save(OrgGroup entity) {
        Map<String, Object> result = new HashMap<String, Object>();
        List<OrgGroup> orgGroupList = query(null);
        result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
        result.put(SystemStaticConst.MSG, "加载组织机构数据成功！");
        result.put("data", orgGroupList);
        return result;
    }

    @Override
    public List<OrgGroup> query(Object o) {
        return null;
    }

    @Override
    public Map<String, Object> loadGroupTree() {
        return null;
    }

    @Override
    public Map<String, Object> remove(OrgGroup entity) {
        Map<String, Object> result = new HashMap<String, Object>();
        if (null == Long.valueOf(entity.getGroupId())) {
            result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
            result.put(SystemStaticConst.MSG, "非法参数！");
            return result;
        }
        try {
            Integer count = findOrgGroupUserCount(entity.getGroupId());
            if (count != null && count > 0) {
                result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
                result.put(SystemStaticConst.MSG, "当前部门下存在人员信息,无法删除！");
                return result;
            }
            boolean b = delete(entity);
            if (b) {
                result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
                result.put(SystemStaticConst.MSG, "删除数据成功！");
            } else {
                result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
                result.put(SystemStaticConst.MSG, "删除数据失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(SystemStaticConst.RESULT, SystemStaticConst.FAIL);
            result.put(SystemStaticConst.MSG, "删除数据失败！");
        }
        return result;
    }

    @Override
    public boolean delete(OrgGroup entity) {
        return false;
    }
}